/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tp_imag.service.impl;

import ij.ImagePlus;
import ij.io.FileSaver;
import ij.process.ByteProcessor;
import ij.process.ImageConverter;
import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.util.Collection;
import javax.imageio.ImageIO;
import tp_imag.model.Filter;
import tp_imag.service.ImageProcessor;

/**
 *
 * @author Administrador
 */
public class ImageProcessorImpl implements ImageProcessor {

    private ColorSpace espacioGrises;
    private ColorConvertOp ccop;


    static float realce[]= {   1, -2, 1
			 , -2, 4, -2
			 , 1, -2, 1};

    static float pasaBajo[]={     0  , 1/10, 0
	 			, 1/10, 6/10, 1/10
	 			,  0  , 1/10, 0};

    static float difuminado[]={     1/16  , 2/16, 1/16
				, 2/16  , 4/16, 2/16
				,  1/16 , 2/16, 1/16};




    public BufferedImage openImage(String imagePath) {
        BufferedImage buf = null;
        try {
            buf = ImageIO.read(new File(imagePath));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return buf;
    }

    public BufferedImage filterImage(BufferedImage source, Collection<Filter> filters) {
        //Inicializamos
        espacioGrises = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ccop = new ColorConvertOp (espacioGrises, null);

        BufferedImage destino = ccop.filter(source, null);
        BufferedImage img = destino;

        boolean primero = true;

        for (Filter filter: filters) {
            img = filter(img, filter.getValues(), primero);
            primero = false;
        }
        if (primero) {
            img = null;
        }
        
        return img;
    }

    private BufferedImage filter(BufferedImage source, float[] values, boolean primero) {
        try {
            //BufferedImage nuevaProbando = new BufferedImage(source.getWidth(),source.getHeight(),BufferedImage.TYPE_BYTE_GRAY);

            ccop = new ColorConvertOp (espacioGrises, null);

            BufferedImage destino = null;
            if (primero) {
                destino = source;
            } else {
                destino = openImage("img_temp/filt.jpg");                
            }

            espacioGrises = ColorSpace.getInstance(ColorSpace.CS_GRAY);
            ccop = new ColorConvertOp(espacioGrises, null);
            BufferedImage dest = ccop.filter(destino, null);
            
            ByteProcessor bp = new ByteProcessor(dest);


            bp.convolve((float[]) values, 3, 3);
            

            //Image im = bp.createImage();

            ImagePlus nueva = new ImagePlus();
            nueva.setProcessor("filtros", bp);
            ImageConverter icNuevo = new ImageConverter(nueva);
            icNuevo.convertToGray8();
            FileSaver fs = new FileSaver(nueva);
            fs.saveAsJpeg("img_temp/filt.jpg");

                        
            return nueva.getBufferedImage();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }        
    }

    public BufferedImage binarizeImage(BufferedImage source) {

        espacioGrises = ColorSpace.getInstance(ColorSpace.CS_GRAY);
	ccop = new ColorConvertOp (espacioGrises, null);

        BufferedImage destino = ccop.filter(source, null);
        //BufferedImage nuevaProbando = new BufferedImage(source.getWidth(),source.getHeight(),BufferedImage.TYPE_BYTE_GRAY);

        ByteProcessor bp = new ByteProcessor(destino);
        //ByteProcessor bp = new ByteProcessor(source);

        bp.autoThreshold();


        ImagePlus nueva = new ImagePlus();
        nueva.setProcessor("binarizada", bp);
        ImageConverter icNuevo = new ImageConverter(nueva);
        icNuevo.convertToGray8();

        return nueva.getBufferedImage();
        //FileSaver fs = new FileSaver(nueva);
        //fs.saveAsJpeg(dirArchivo+"/"+nombreArchivo+"_"+filtro+".jpg");

    }

    
}
